#include<iostream>
#include<vector>
using namespace std;
int main(){
  int m,n,x,y;
  cin>>m>>n;
  cin>>x>>y;
  auto instruct=vector(n,vector(m,pair<int,int>()));
  for(auto &line:instruct){
    for(auto &[x,y]:line) cin>>x>>y;
  }
  do{
    cout<<x<<" "<<y<<endl;
    // now.x=instruct[i][j].first;
    // now.y=instruct[i][j].second;
    tie(x, y) = instruct[x-1][y-1];
  }while(x!=0&&y!=0);
}